package edu.stanford.bmir.protege.web.server.stkosutil.mapper;

import java.util.Comparator;

import edu.stanford.bmir.protege.web.client.rpc.stkosutil.common.MatchLevel;

public class OwlClassHolderComparator implements Comparator<OwlClassHolder> {

	private MatchLevel matchLevel= MatchLevel.CASE_INSENSITIVE;
	
	public OwlClassHolderComparator(MatchLevel matchLevel){
		this.matchLevel = matchLevel;
	}
	public void setMatchLevel(MatchLevel matchLevel){
		this.matchLevel = matchLevel;
	}

	public int compare(OwlClassHolder o1, OwlClassHolder o2) {
		String name1 = o1.getBrowserText();
		String name2 = o2.getBrowserText();
		name1 = (name1==null)?"":name1;
		name2 = (name2==null)?"":name2;
		if (this.matchLevel==MatchLevel.CASE_SENSITIVE){
			return name1.compareTo(name2);
		}else if (this.matchLevel==MatchLevel.CASE_INSENSITIVE){
			return name1.compareToIgnoreCase(name2);
		}else if (this.matchLevel==MatchLevel.INCLUSIVE){
			name1 = name1.toLowerCase();
			name2 = name2.toLowerCase();
			if (name1.indexOf(name2)!=-1 || name2.indexOf(name1)!=-1){
				return 0;
			}else{
				return name1.compareToIgnoreCase(name2);
			}
		}else{
			return name1.compareToIgnoreCase(name2);
		}
	}

}
